package com.dmz.wxcp.wecom.wxbusiness.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmz.wxcp.constants.Constant;
import com.dmz.wxcp.redis.RedisUtils;
import com.dmz.wxcp.wecom.common.service.RedisService;
import com.dmz.wxcp.wecom.wxbusiness.entity.Article;
import com.dmz.wxcp.wecom.wxbusiness.entity.ArticleTags;
import com.dmz.wxcp.wecom.wxbusiness.mapper.ArticleTagsMapper;
import com.dmz.wxcp.wecom.wxbusiness.mapper.FactoryMapper;
import com.dmz.wxcp.wecom.wxbusiness.service.FactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class FactoryServiceImpl extends ServiceImpl<FactoryMapper, Article> implements FactoryService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private ArticleTagsMapper tagsMapper;

    @Override
    public Map<String, Object> getTags() {
        //        分别获取热门标签和最新标签 先从redis中获取
        List<ArticleTags> hottags=new ArrayList<>();
        List<ArticleTags> newtagslist=new ArrayList<>();
        String hot= Constant.MOBILE_HOT_FACTORYARTICLETAGLIST;
        String newtags= Constant.MOBILE_NEW_FACTORYARTICLETAGLIST;
        if(redisService.hasKey(hot)){
            hottags= RedisUtils.RedisToObjectList(redisService.get(hot).toString(), ArticleTags.class);
        }else{
            hottags=tagsMapper.getHotArticleTag(8);
            redisService.set(hot,hottags,2, TimeUnit.MINUTES);
        }

        if(redisService.hasKey(newtags)){
            newtagslist= RedisUtils.RedisToObjectList(redisService.get(newtags).toString(), ArticleTags.class);
        }else{
            newtagslist=tagsMapper.getNewArticleTag(8);
            redisService.set(newtags,newtagslist,2, TimeUnit.MINUTES);
        }

        Map<String,Object> map=new HashMap<>();
        map.put("hot",hottags);
        map.put("new",newtagslist);
        return map;

    }
}
